/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-15 02:55:42
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import zyy.util.Bus;
import zyy.util.BusinessException;

public class FrmUserNewPwd extends JDialog implements ActionListener {
  private static final long serialVersionUID = 6874159025403864L;
  private JPanel text = new JPanel();
  private JPanel btns = new JPanel();
  private JLabel oriPwdlabel = new JLabel("原密码：");
  private JPasswordField oriPwdEdt = new JPasswordField(20);
  private JLabel n1Pwdlabel = new JLabel("新密码：");
  private JPasswordField n1PwdEdt = new JPasswordField(20);
  private JLabel n2Pwdlabel = new JLabel("重新输入：");
  private JPasswordField n2PwdEdt = new JPasswordField(20);
  private JButton commit = new JButton("提交");
  private JButton exit = new JButton("退出");

  public String oriPwd;
  public String n1Pwd;
  public String n2Pwd;

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.commit) {
      this.oriPwd = new String(this.oriPwdEdt.getPassword());
      this.n1Pwd = new String(this.n1PwdEdt.getPassword());
      this.n2Pwd = new String(this.n2PwdEdt.getPassword());
      try {
        Bus.userManager.changePasswd(Bus.currentUser.getUserID(), oriPwd, n1Pwd, n2Pwd);
        // Bus.userManager.loadUserInfo(Bus.fUserInfo);
        JOptionPane.showMessageDialog(null, String.format("密码修改成功\n原密码 ——> 新密码\n %s ——> %s", oriPwd, n1Pwd), "成功",
            JOptionPane.INFORMATION_MESSAGE);
        this.setVisible(false);
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    } else if (e.getSource() == this.exit) {
      this.setVisible(false);
    }
  }

  public FrmUserNewPwd() {
    // 基础 JDialog 设置
    {
      this.setTitle("外卖助手 用户 更改密码");
      this.setSize(355, 200);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
      // Bus.fNewPD = this;
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.text, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
    }
    // text 相关配置
    {
      this.text.setLayout(new FlowLayout(FlowLayout.LEFT));
      this.text.add(this.oriPwdlabel);
      this.text.add(this.oriPwdEdt);
      this.text.add(this.n1Pwdlabel);
      this.text.add(this.n1PwdEdt);
      this.text.add(this.n2Pwdlabel);
      this.text.add(this.n2PwdEdt);
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.commit);
      this.btns.add(this.exit);
      this.commit.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.commit);
      Bus.jButtonZYYStyle(this.exit);
    }
  }
}